<template>
  <div style="width: 800px; margin: auto">
    <h1>用户注册</h1>
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
    >
      <el-form-item label="手机号" prop="userMobile">
        <el-input v-model="ruleForm.userMobile" />
      </el-form-item>
      <el-form-item label="短信验证码" required>
        <el-col :span="11">
          <el-form-item prop="code">
            <el-input v-model="ruleForm.code" />
          </el-form-item>
        </el-col>
        <el-col class="text-center" :span="2">
          <span class="text-gray-500"></span>
        </el-col>
        <el-col :span="11">
          <el-form-item>
            <el-button
              type="primary"
              @click="onSendCode(ruleFormRef)"
              :disabled="disabled"
              >{{
                disabled ? seconds + "秒后重新获取" : "发送验证码"
              }}</el-button
            >
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="密码" prop="userPass">
        <el-input v-model="ruleForm.userPass" show-password />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          立即注册
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
import { sendCode, regUser } from "../api/api";
import { useRouter } from "vue-router";

const router = useRouter();

const disabled = ref(false);
const seconds = ref(60);

interface RuleForm {
  userMobile: string;
  code: string;
  userPass: string;
}

const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
  userMobile: "",
  code: "",
  userPass: "",
});

const locationOptions = ["Home", "Company", "School"];

const rules = reactive<FormRules<RuleForm>>({
  userMobile: [
    { required: true, message: "手机号不能为空", trigger: "blur" },
    {
      pattern:
        /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
      message: "请输入正确的手机号",
      trigger: "blur",
    },
  ],
  code: [{ required: true, message: "验证码不能为空", trigger: "blur" }],
  userPass: [{ required: true, message: "密码不能为空", trigger: "blur" }],
});
//发送验证码
const onSendCode = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validateField(["userMobile"], (valid, fields) => {
    if (valid) {
      console.log("submit!");
      //倒计时60秒禁用
      disabled.value = true;
      let time = setInterval(() => {
        if (seconds.value > 0) {
          seconds.value--;
        } else {
          disabled.value = false;
          seconds.value = 60;
          clearInterval(time);
        }
      }, 1000);
      sendCode(ruleForm.userMobile).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("发送成功");
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      regUser(ruleForm).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("注册成功");
          router.push("/login");
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<style scoped></style>
